package flowcontrol.countdownlatch;


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 描述：模拟百米赛跑，5个运动员都已经准备好，只等裁判员一声令下，所有运动员同时开始跑步
 */

public class CountDownLatchDemo02 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);// 发令枪只响一次
        ExecutorService service = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            service.submit(()->{
                try {
                    System.out.println("运动员" + Thread.currentThread().getName() + "准备好了");
                    latch.await();// 等待发令枪响
                    System.out.println("运动员" + Thread.currentThread().getName() + "开始跑步");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        Thread.sleep((long) (Math.random()*10000));
        System.out.println("发令枪响，比赛开始！");
        latch.countDown();// 发令枪响
    }
}
